

#include <stdint.h>
#include <string.h> 


const uint8_t HEX_CODE[] = {"0123456789ABCDEF"};

int uint32_to_string(uint32_t val, uint8_t* data)
{
//    uint8_t *p = data;
    int index = 0; 
    if(val > 99999){
        data[index ++] = val / 100000 + '0';
    }
    if(val > 9999){
        data[index ++] = val % 100000 / 10000 + '0';
    }
    if(val > 999){
        data[index ++] = val % 10000/1000 + '0';
    }
    if(val > 99){
        data[index ++] = val % 1000 / 100 + '0';
    }
    if(val > 9){
        data[index ++] = val % 100 / 10 + '0';
    }
    data[index ++] = val % 10 + '0';
    data[index ] = 0;

    return index;
}

int uint16_to_string(uint16_t val, uint8_t* data)
{
    int index = 0; 
    if(val > 9999){
        data[index ++] = val / 10000 + '0';
    }
    if(val > 999){
        data[index ++] = val % 10000/1000 + '0';
    }
    if(val > 99){
        data[index ++] = val % 1000 / 100 + '0';
    }
    if(val > 9){
        data[index ++] = val % 100 / 10 + '0';
    }
    data[index ++] = val % 10 + '0';
    data[index ] = 0;

    return index;
}

int conv_uint8_to_string(uint8_t val, uint8_t *data)
{
    int count = 0; 

    if(val > 99){
        data[count ++] = val / 100 + '0';
    }
    if(val > 9){
        data[count ++] = val % 100 / 10 + '0';
    }
    data[count ++] = val % 10 + '0';
    data[count] = 0;

    return count;
}
void conv_uint8_to_string_format(uint8_t val, uint8_t *data, int len, char pattern)
{
    int offset = len ;
    data[offset--] = 0;

    while(val > 0 && offset > -1)
    {
        data[offset] = val % 10 + '0';
        val /= 10;
        offset --; 
    }

    for(int i = 0; i <= offset; i++){
        data[i] = pattern;
    }
}

void uint32_to_hex_string(uint32_t val, uint8_t *data)
{
    uint32_t offset = 32 - 4;
//    uint8_t *p = data; 
    int temp;
    for(int i = 0; i < 8; i++){
        temp = ((val >> offset) & 0x0f) ;
        offset -= 4; 
        data[i] = HEX_CODE[ temp ];        
    }
}
void uint8_to_hex_string(uint8_t val, uint8_t *data)
{
    data[0] = HEX_CODE[ (val >> 4) & 0x0f];
    data[1] = HEX_CODE[ val & 0x0f ];
}

//char stringRecord[32];
int uint8_to_percent(uint8_t val, uint8_t *data)
{
    data[0] = val / 100 + '0';
    data[1] = val % 100 / 10 + '0';
    data[2] = val % 10 + '0'; 
    data[3] = '%';
    data[4] = 0;

    if( val < 100){
        data[0] = ' ';
    }
    if( val < 10){
        data[1] = ' ';
    }
    return 4; 
}
int conv_to_battery_level(uint8_t val, uint8_t *data)
{
    if( val > 99){
        data[0] = '1';
        data[1] = '0';
        data[2] = '0';
        data[3] = '%';
        data[4] = 0;
        return 4;
    }else if( val > 9){
        data[0] = val / 10 + '0';
        data[1] = val % 10 + '0';
        data[2] = '%';
        data[3] = 0;
        return 3;
    }else{
        data[0] = val + '0';
        data[1] = '%';
        data[2] = 0;
        return 2; 
    }
}

// static uint8_t tp_tip_val = 0; 
int conv_to_tp_tip_format(uint8_t val, uint8_t *data)
{
    // tp_tip_val = val; 
    
    data[0] = 'T';
    data[1] = 'P';
    data[2] = ':';
    int count = conv_uint8_to_string( val, data + 3);
    data[count + 3] = '%';
    data[count + 4] = 0; 

    return count + 4; 
}

int conv_to_notify_date(uint8_t month, uint8_t day, uint8_t *data)
{
    data[0] = month / 10 + '0';
    data[1] = month % 10 + '0';
    data[2] = '-';
    data[3] = day / 10 + '0';
    data[4] = day % 10 + '0';
    data[5] = 0;
    return 5;
}
int conv_to_notify_date_str(uint8_t * date, uint8_t *data)
{
    data[0] = date[0];
    data[1] = date[1];
    data[2] = '-';
    data[3] = date[2];
    data[4] = date[3];
    data[5] = 0;
    return 5;
}
int conv_to_notify_time_str(uint8_t *time, uint8_t *data)
{
    data[0] = time[0];
    data[1] = time[1];
    data[2] = ':';
    data[3] = time[2];
    data[4] = time[3]; 
    data[5] = 0; 
    return 5; 
}

int conv_to_notify_time(uint8_t hour, uint8_t minute, uint8_t *data)
{
    data[0] = hour / 10 + '0';
    data[1] = hour % 10 + '0';
    data[2] = ':';
    data[3] = minute / 10 + '0';
    data[4] = minute % 10 + '0';
    data[5] = 0;
    return 5; 
}

int conv_to_decimal_datetime(uint8_t data, uint8_t *array )
{
    array[0] = data / 10 + '0';
    array[1] = data % 10 + '0';
    array[2] = 0;
    return 2; 
}
int conv_to_decimal_month_day(uint8_t month, uint8_t day, uint8_t *array)
{
    array[0] = month / 10 + '0';
    array[1] = month % 10 + '0';
    array[2] = '-';
    array[3] = day / 10 + '0';
    array[4] = day % 10 + '0';
    array[5] = 0; 
    return 5;
}
int conv_to_decimal_time(uint8_t hour, uint8_t minute, uint8_t second, uint8_t *array)
{
    array[0] = hour / 10 + '0';
    array[1] = hour % 10 + '0';
    array[2] = ':';
    array[3] = minute / 10 + '0';
    array[4] = minute % 10 + '0';
    array[5] = ':';
    array[6] = second / 10 + '0';
    array[7] = second % 10 + '0';
    array[8] = 0;
    return 8; 
}


int conv_to_sleep_datetime(uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t *array)
{
    array[0] = day / 10 + '0';
    array[1] = day % 10 + '0';
    array[2] = '/';
    array[3] = month / 10 + '0';
    array[4] = month % 10 + '0';
    array[5] = ' ';

    array[6] = hour / 10 + '0';
    array[7] = hour % 10 + '0';
    array[8] = ':';
    array[9] = minute / 10 + '0';
    array[10] = minute % 10 + '0';
    array[11] = 0;

    return 11;     
}

int conv_to_sleep_time(uint16_t time, uint8_t *array)
{
    uint8_t  desc[] = {"00Hour 00Min"};
    uint8_t hour = time / 60;
    uint8_t minute = time % 60;

    if( hour == 0){
        desc[7] = minute / 10 + '0';
        desc[8] = minute % 10 + '0';
    }
    else if( minute == 0){
        desc[0] = hour / 10 + '0';
        desc[1] = hour % 10 + '0';
    }
    else 
    {
        desc[0] = hour / 10 + '0';
        desc[1] = hour % 10 + '0';
        desc[7] = minute / 10 + '0';
        desc[8] = minute % 10 + '0';
    }
    memcpy( array, desc, 12);

    return 12; 
}

int conv_to_heartrate_bpm(uint8_t bpm, uint8_t *array)
{
    if( bpm == 0){
        array[0] = array[1] = array[2] = '-';
        array[3] = 0;
        return 3; 
    }

    // int index = 0; 
    if(bpm > 99){
        array[0] = bpm / 100 + '0';
    }else{
        array[0] = ' ';
    }
    array[1] = bpm % 100 / 10 + '0';
    array[2] = bpm % 10 + '0';
    array[3] = 0; 
    return 3;
}

int conv_to_heartrate_measure(uint8_t bpm, uint8_t *array)
{
    if( bpm == 0){
        array[0] = array[1] = array[2] = '-';
        array[3] = 0; 
        return 3; 
    }

    if(bpm > 99){
        array[0] = bpm / 100 + '0';
    }else{
        array[0] = ' ';
    }
    array[1] = bpm % 100 / 10 + '0';
    array[2] = bpm % 10 + '0';
    array[3] = 0;
    return 4; 
}

int conv_to_version(uint32_t version, uint8_t *array)
{
    int count = 1;
    array[0] = 'v';
    count += conv_uint8_to_string((version >> 24), array + count); // 1 
    array[count ++] = '.';
    count += conv_uint8_to_string((version >> 16), array + count);
    array[count ++] = '.';
    count += conv_uint8_to_string((version >> 8), array + count);
    array[count ++] = 0;

    return count; 
}
int conv_to_mac(uint8_t *mac, uint8_t *array)
{
    array[0] = 'M';
    array[1] = 'A';
    array[2] = 'C';
    array[3] = ':';

    for(int i = 0; i < 6; i ++)
    {
        array[4 + 3 * i] = HEX_CODE[ (mac[5-i] >> 4) & 0x0F ];
        array[5 + 3 * i] = HEX_CODE[ (mac[5-i] >> 0) & 0x0F ];
        array[6 + 3 * i] = '-';
    }
    // 6 * 3 = 18 + 4 = 22
    array[21] = 0;
    return 21; 
}

int conv_to_ft_heartrate_raw(uint16_t raw, uint8_t *data)
{
    if( raw == 0xffff ){
        data[0] = data[1] = data[2] = '-';
        data[3] = 0;
        return 3;
    }

    return uint16_to_string(raw, data); 
}
